package icom.acm.util;

import icom.acm.util.mysql.MysqlDB;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;

public class DBConnect {
	protected Connection conn = null;
	private Statement stm = null;
	private PreparedStatement pstmt = null;
	private ResultSet rst = null;

	public void init() {
		try {
			MysqlDB mysql = MysqlDB.newInstance();
			conn = mysql.getConnection();			
			/*DBConfig config = DBConfig.newInstance();
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			final String url = config.getUrl();		
			this.conn = DriverManager.getConnection(url, config.getUsername(),
					config.getPassword());*/
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public DBConnect() {
		try {
			this.init();
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}
	
	public DBConnect(final String s) {
		try {
			this.init();
			this.prepareStatement(s);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setAutoCommit(final boolean flag) {
		try {
			if (this.conn != null) {
				;
			}
			this.conn.setAutoCommit(flag);
		} catch (final SQLException e) {
			e.printStackTrace();
		}
	}

	public void commit() {
		try {
			if (this.conn != null) {
				this.conn.commit();
			}
		} catch (final SQLException e) {
			e.printStackTrace();
		}
	}

	public void excuteQuery() {
		try {
			if (this.pstmt != null) {
				this.rst = this.pstmt.executeQuery();
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public boolean excute() throws com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException {
		try {
			return this.pstmt.execute();
		} catch (final SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	public int excuteUpdate() {
		int i = 0;
		try {
			if (this.pstmt != null) {
				i = this.pstmt.executeUpdate();
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
		return i;
	}

	public void getResultSet() {
		try {
			this.rst = this.pstmt.getResultSet();
		} catch (final SQLException e) {
			e.printStackTrace();
		}
	}

	public boolean next() {
		try {
			return this.rst.next();
		} catch (final NullPointerException e) {
			return false;
		} catch (final Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	// ȡֵ����
	public boolean getBoolean(final int i) throws Exception {
		return this.rst.getBoolean(i);
	}

	public boolean getBoolean(final String s) throws Exception {
		return this.rst.getBoolean(s);
	}

	public byte getByte(final int i) throws Exception {
		return this.rst.getByte(i);
	}

	public byte getByte(final String s) throws Exception {
		return this.rst.getByte(s);
	}

	public byte[] getBytes(final int i) throws Exception {
		return this.rst.getBytes(i);
	}

	public byte[] getBytes(final String s) throws Exception {
		return this.rst.getBytes(s);
	}

	public Date getDate(final int i) throws Exception {
		return this.rst.getDate(i);
	}

	public Date getDate(final String s) throws Exception {
		return this.rst.getDate(s);
	}

	public Time getTime(final int i) throws Exception {
		return this.rst.getTime(i);
	}

	public Time getTime(final String s) throws Exception {
		return this.rst.getTime(s);
	}

	public double getDouble(final int i) throws Exception {
		return this.rst.getDouble(i);
	}

	public double getDouble(final String s) throws Exception {
		return this.rst.getDouble(s);
	}

	public float getFloat(final int i) throws Exception {
		return this.rst.getFloat(i);
	}

	public float getFloat(final String s) throws Exception {
		return this.rst.getFloat(s);
	}

	public int getInt(final int i) throws Exception {
		return this.rst.getInt(i);
	}

	public int getInt(final String s) throws Exception {
		return this.rst.getInt(s);
	}

	public long getLong(final int i) throws Exception {
		return this.rst.getLong(i);
	}

	public long getLong(final String s) throws Exception {
		return this.rst.getLong(s);
	}

	public short getShort(final int i) throws Exception {
		return this.rst.getShort(i);
	}

	public short getShort(final String s) throws Exception {
		return this.rst.getShort(s);
	}

	public String getString(final int i) throws Exception {
		return this.rst.getString(i);
	}

	public String getString(final String s) throws Exception {
		return this.rst.getString(s);
	}

	public InputStream getBlob(final String i) throws SQLException {
		return this.rst.getBinaryStream(i);

	}

	public InputStream getBlob(final int i) throws SQLException {
		return this.rst.getBinaryStream(i);
	}
	
	public Object getObj(final String s) throws SQLException{
		return this.rst.getObject(s);
	}
	
	public Object getObj(final int i) throws SQLException{
		return this.rst.getObject(i);
	}

	// ��ֵ����
	public void setString(final int i, final String s) {
		try {
			this.pstmt.setString(i, s);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setBoolean(final int i, final boolean flag) {
		try {
			this.pstmt.setBoolean(i, flag);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setByte(final int i, final byte byte0) {
		try {
			this.pstmt.setByte(i, byte0);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setBytes(final int i, final byte abyte0[]) {
		try {
			this.pstmt.setBytes(i, abyte0);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setDate(final int i, final Date date) {
		try {
			this.pstmt.setDate(i, date);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setTimestamp(final int i, final Timestamp timestamp) {
		try {
			this.pstmt.setTimestamp(i, timestamp);
		} catch (final SQLException e) {
			e.printStackTrace();
		}
	}

	public void setTime(final int i, final Time time) {
		try {
			this.pstmt.setTime(i, time);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setShort(final int i, final short word0) {
		try {
			this.pstmt.setShort(i, word0);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setInt(final int i, final int j) {
		try {
			this.pstmt.setInt(i, j);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setLong(final int i, final long l) {
		try {
			this.pstmt.setLong(i, l);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setFloat(final int i, final float f) {
		try {
			this.pstmt.setFloat(i, f);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public void setDouble(final int i, final double d) {
		try {
			this.pstmt.setDouble(i, d);
		} catch (final Exception e) {
			e.printStackTrace();
		}

	}

	public void setBlob(final int i, final InputStream is, final int len) {
		try {
			this.pstmt.setBinaryStream(i, is, len);
		} catch (final Exception ex) {
			ex.printStackTrace();
		}
	}

	//    
	public void createStatement() {
		try {
			this.stm = this.conn.createStatement();
		} catch (final SQLException e) {
			e.printStackTrace();
		}
	}

	public void prepareStatement(final String sql, final int type) {
		try {
			this.pstmt = this.conn.prepareStatement(sql,
					Statement.RETURN_GENERATED_KEYS);
		} catch (final SQLException e) {
			e.printStackTrace();
		}
	}

	public void prepareStatement(final String s) {
		try {
			if(pstmt!=null){
				if (!pstmt.isClosed()){
					pstmt.close();
					pstmt = null;
				}
			}
			this.pstmt = this.conn.prepareStatement(s);
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public int getGeneratedKeys(){
		ResultSet rsKey = null;
		int key = 0;
		try {
			rsKey = pstmt.getGeneratedKeys();
			rsKey.next();  
			key = rsKey.getInt(1);
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				if (rsKey!=null)
					rsKey.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return key;
	}
	
	public boolean isClose(){
		try {
		if (rst==null || rst.isClosed());
		else if (stm==null || stm.isClosed());
		else if (pstmt==null || pstmt.isClosed());
		else if (conn==null || conn.isClosed())
			return true;
		} catch(Exception ex){ex.printStackTrace();}		
		return false;
	}
	
	public void close() {
		try {
			if (this.rst != null) {
				this.rst.close();
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
		try {
			if (this.stm != null) {
				this.stm.close();
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
		try {
			if (this.pstmt != null) {
				this.pstmt.close();
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
		try {
			if (!this.conn.isClosed()) {
				this.conn.close();
			}
		} catch (final Exception e) {
			e.printStackTrace();
		}
	}

	public Timestamp getTimestamp(final String s) throws SQLException {
		return this.rst.getTimestamp(s);
	}
}
